﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TwitterChess;
using MooneyTool;

namespace WindowsFormsApplication1
{
    public partial class TwitterChess : Form
    {
        private Tweety Twitter;
        private GameManager Manager;

        public TwitterChess()
        {
            InitializeComponent();

            Twitter = new Tweety();
            Manager = new GameManager();

            Twitter.TwitterMessageReceived += new Tweety.TwitterMessageHandler(this.ReadTweet);
            Twitter.TwitterMessageReceived += new Tweety.TwitterMessageHandler(Manager.ReceiveMessage);

            Manager.AIMoveMade += new GameManager.AIMoveMadeHandler(Twitter.SendMessageDelegate);
            Manager.AIMoveMade += new GameManager.AIMoveMadeHandler(this.LogAIMove);

        }

        private void TwitterChess_Load(object sender, EventArgs e)
        {

        }

        private void btnTwitterValidation_Click(object sender, EventArgs e)
        {
            Twitter.Authenticate(txtTwitterValidation.Text);

            if (Twitter.IsAuthenticated)
            {
                lblTwitterStatus.Text = "Succesfully authenticated.";
            }
            else
            {
                lblTwitterStatus.Text = "Failed to authenticate, please try again.";

                Twitter = new Tweety();
            }
        }

        public void AppendTwitterHisttory(String message)
        {
            txtMoveHistory.Invoke((MethodInvoker)(() => txtMoveHistory.Text = message + "\r\n" + txtMoveHistory.Text));
        }

        private void LogAIMove(object feedback, AIMoveMadeArgs args)
        {
            AppendTwitterHisttory("AI to " + args.opponent.twitterUserName + ": " + args.message);
        }

        private void ReadTweet(object sender, TweetReceivedEventArgs args)
        {
            Logger.getInstance().Add(args.message);
            AppendTwitterHisttory("Opponent " + sender + ": " + args.message);
        }
		private void btnBotMatch_Click(object sender, EventArgs e)
		{
			Manager.NewGame(new TwitterOpponent(txtBotMatch.Text));
			Twitter.SendMessage("@"+ txtBotMatch.Text + " start bot match (" + DateTime.Now.ToShortTimeString() + ")");
		}
    }
}
